<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:dept:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:dept:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:dept:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:dept:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table 
    v-loading="loading" 
    :data="deptList" 
    @selection-change="handleSelectionChange"
      row-key="workshopId"
    
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="车间id" align="center" prop="workshopId" />
      <el-table-column label="部门id" align="center" prop="parentId" /> -->
         <el-table-column label="部门全名" align="center" prop="fullName" />
      <el-table-column label="部门代码" align="center" prop="code" />
   
      <el-table-column label="车间代码" align="center" prop="workshopCode" />
      <el-table-column label="车间名称" align="center" prop="workshopName" />
      <el-table-column label="部门主管" align="center" prop="headDept" />
      <el-table-column label="部门电话" align="center" prop="deptTelephone" />
      <el-table-column label="传真" align="center" prop="fax" />
      <el-table-column label="部门属性" align="center" prop="deptAttribute" />
      <el-table-column label="是否进行信用管理" align="center" prop="creditManagement" />
      <el-table-column label="核算科目代码" align="center" prop="accountingAccountCode" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="成本核算类型" align="center" prop="costAccountingType" />
      <el-table-column label="是否虚拟部门" align="center" prop="virtualDept" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:dept:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:dept:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="上级部门" prop="parentId">
              <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级部门" />
            </el-form-item> 
  
        <el-form-item label="部门代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入部门代码" />
        </el-form-item>
        <el-form-item label="部门全名" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入部门全名" />
        </el-form-item>
        <!-- <el-form-item label="车间代码" prop="workshopCode">
          <el-input v-model="form.workshopCode" placeholder="请输入车间代码" />
        </el-form-item>
        <el-form-item label="车间名称" prop="workshopName">
          <el-input v-model="form.workshopName" placeholder="请输入车间名称" />
        </el-form-item> -->
        <el-form-item label="部门主管" prop="headDept">
          <el-input v-model="form.headDept" placeholder="请输入部门主管" />
        </el-form-item>
        <el-form-item label="部门电话" prop="deptTelephone">
          <el-input v-model="form.deptTelephone" placeholder="请输入部门电话" />
        </el-form-item>
        <el-form-item label="传真" prop="fax">
          <el-input v-model="form.fax" placeholder="请输入传真" />
        </el-form-item>
        <el-form-item label="部门属性" prop="deptAttribute">
           <el-select v-model="form.deptAttribute" placeholder="请输入部门属性">
    <el-option
      v-for="item1 in deptAttributeOptions"
      :key="item1.value"
      :label="item1.label"
      :value="item1.value">
    </el-option>
  </el-select>
        </el-form-item>
        <el-form-item label="是否进行信用管理" prop="creditManagement">
                     <el-select v-model="form.creditManagement" placeholder="请选择是否进行信用管理">
    <el-option
      v-for="item1 in creditManagementOptions"
      :key="item1.value"
      :label="item1.label"
      :value="item1.value">
    </el-option>
  </el-select>
        </el-form-item>
        <el-form-item label="核算科目代码" prop="accountingAccountCode">
          <el-input v-model="form.accountingAccountCode" placeholder="请选择核算科目代码" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="成本核算类型" prop="costAccountingType">
          <el-input v-model="form.costAccountingType" placeholder="请输入成本核算类型" />
        </el-form-item>
        <el-form-item label="是否虚拟部门" prop="virtualDept">
    <el-select v-model="form.virtualDept" placeholder="请选择是否虚拟部门">
    <el-option
      v-for="item1 in virtualDeptOptions"
      :key="item1.value"
      :label="item1.label"
      :value="item1.value">
    </el-option>
  </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeptExcludeChild,listDept, getDept, delDept, addDept, updateDept, exportDept } from "@/api/base/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Dept",
  components: {Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
           // 部门树选项
      deptOptions: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格树数据
      deptList: [],
      // 弹出层标题
      title: "",
      //部门属性
      deptAttributeOptions:[{
          value: '车间',
          label: '车间'
        }, {
          value: '非车间',
          label: '非车间'
        },],
        //是否进行信用管理
        creditManagementOptions:[{
          value: '是',
          label: '是'
        }, {
          value: '否',
          label: '否'
        },],
        //是否虚拟部门
        virtualDeptOptions:[{
          value: '是',
          label: '是'
        }, {
          value: '否',
          label: '否'
        },],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: undefined,
        code: undefined,
        fullName: undefined,
        workshopCode: undefined,
        workshopName: undefined,
        headDept: undefined,
        deptTelephone: undefined,
        fax: undefined,
        deptAttribute: undefined,
        creditManagement: undefined,
        accountingAccountCode: undefined,
        remarks: undefined,
        costAccountingType: undefined,
        virtualDept: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      listDept(this.queryParams).then(response => {
        this.deptList = this.handleTree(response.data, "workshopId");
      
        this.loading = false;
      });
    },
     /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.workshopId,
        label: node.fullName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workshopId: undefined,
        parentId: undefined,
        code: undefined,
        fullName: undefined,
        workshopCode: undefined,
        workshopName: undefined,
        headDept: undefined,
        deptTelephone: undefined,
        fax: undefined,
        deptAttribute: undefined,
        creditManagement: undefined,
        accountingAccountCode: undefined,
        remarks: undefined,
        costAccountingType: undefined,
        virtualDept: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workshopId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
      handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.deptId;
      }
      this.open = true;
      this.title = "添加部门";
      listDept().then(response => {
          this.deptOptions = this.handleTree(response.data, "workshopId");
          
      });
    },
   /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
       const workshopId=row.workshopId || this.ids
      getDept(workshopId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改部门";
      });
       listDeptExcludeChild(workshopId).then(response => {
	         this.deptOptions = this.handleTree(response.data, "workshopId");
       });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if(this.form.parentId==null){
          this.form.parentId=0;

        }



        if (valid) {
          if (this.form.workshopId != null) {
            updateDept(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDept(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workshopIds = row.workshopId || this.ids;
      this.$confirm('是否确认删除部门编号为"' + workshopIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDept(workshopIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有部门数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDept(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>